package com.example.demo.test;

/**
 * @Description TODO
 * @Author wzh
 * @Date 2022/6/7 9:39 下午
 * @Version 1.0
 */
public class Lc2 {
    public static void main(String[] args) {
        ListNode l111 = new ListNode(3);
        ListNode l11 = new ListNode(4,l111);
        ListNode l1 = new ListNode(2,l11);

        ListNode l222 = new ListNode(5);
        ListNode l22 = new ListNode(6,l222);
        ListNode l2 = new ListNode(7,l22);
    }

    /**
     * 链表相加
     * 定义临时的操作节点，定义当时操作的节点
     * cur.next = node.val
     * 节点往下传递  cur = cur.next
     *
     *
     * @param l1
     * @param l2
     * @return
     */
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode result = new ListNode(0);
        ListNode p1 = l1;
        ListNode p2 = l2;
        ListNode cur = result;
        int jw = 0;
        while (p1!= null || p2!= null){
            int x = p1 != null?p1.val:0;
            int y = p2 != null?p2.val:0;
            int sum = x + y+jw;
            jw = sum / 10;
            cur.next = new ListNode(sum%10);
            cur = cur.next;
            if(p1!=null)p1 = p1.next;
            if(p2!=null)p2 = p2.next;
        }
        if(jw >0){
            cur.next = new ListNode(jw);
        }
        return result.next;
    }



}

class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
